package com.lxp.kotlintest.presenter

import android.util.Log
import com.lxp.kotlintest.base.BasePresenter
import com.lxp.kotlintest.contract.NewsContract
import com.lxp.kotlintest.http.Exception.ExceptionHandle
import com.lxp.kotlintest.http.Network
import com.lxp.kotlintest.model.NewsMosel
import com.orhanobut.logger.Logger
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class NewsPresenter : BasePresenter<NewsContract.View>(), NewsContract.Presenter {
    override fun getNewsList() {
        checkViewAttached()

        mRootView?.showLoading()


        val disposable = mNesModel.getNewsList("top", "bdd57572004d466f998f6c06448d3d78")
            .subscribe(
                { issue ->
                    mRootView?.apply {
                        dismissLoading()
                        if (issue.error_code == 0) {
                            getNewsList(issue)
                        } else {
                            showError(issue.reason, issue.error_code, 1)
                        }
                    }
                }, { throwable ->
                    mRootView?.apply {
                        dismissLoading()
                        //处理异常
                        showError(ExceptionHandle.handleException(throwable), ExceptionHandle.errorCode, 1)
                    }
                })

        addSubscription(disposable)
    }

    private val mNesModel by lazy { NewsMosel() }



}